<?php

namespace app\api\model\house;

use think\Db;
use think\Model;


class HouseEvaluate extends Model
{





    // 表名
    protected $name = 'house_evaluate';

    // 自动写入时间戳字段
    protected $autoWriteTimestamp = 'integer';

    // 定义时间戳字段名
    protected $createTime = 'createtime';
    protected $updateTime = 'updatetime';
    protected $deleteTime = false;

    // 追加属性
    protected $append = [

    ];

    //业主回复
    public function reply($params)
    {
        $evaluate_id = $params['evaluate_id'];
        $video = $params['video'];
        $content = $params['content'];
        $images = $params['images'];

        $owner_id = $params['owner_id'];
        //$order_info = HouseOrder::get(['order_id'=>$order_id,'owner_id'=>$owner_id]);
        //if (!$order_info)throw new \Exception('error');
        $info = HouseEvaluate::get($evaluate_id);
        if (!$info)throw new \Exception('评价记录不存在');
        if ($info['is_reply']==1)throw new \Exception('该评价已回复');

        $evaluate_model = new HouseEvaluate;

        $data = [
            'owner_reply_content'=>$content,
            'owner_images'=>$images,
            'owner_video'=>$video,
            'is_reply'=>1
        ];

        $res = $evaluate_model->save($data,['evaluate_id'=>$evaluate_id]);

        return $res;
    }

    //用户评价
    public function add($params)
    {
        $video = $params['video'];
        $content = $params['content'];
        $images = $params['images'];
        $order_id = $params['order_id'];
        $user_id = $params['user_id'];
        $describe_score = $params['describe_score'];
        $location_score = $params['location_score'];
        $convenient_score = $params['convenient_score'];
        $service_score = $params['service_score'];
        $health_score = $params['health_score'];
        $cost_performance_score = $params['cost_performance_score'];
        $order_info = HouseOrder::get($order_id);
        if (!$order_info)throw new \Exception('房屋订单不存在');
        $owner_id = $order_info['owner_id'];
        $house_id = $order_info['house_id'];
        if (!$order_id&&!$owner_id)throw new \Exception('参数缺少');
        if (empty($content))throw new \Exception('请输入评价内容');
        if (!$describe_score)throw new \Exception('请选择描述评分');
        if (!$location_score)throw new \Exception('请选择位置评分');
        if (!$convenient_score)throw new \Exception('请选择便捷评分');
        if (!$service_score)throw new \Exception('请选择服务评分');
        if (!$health_score)throw new \Exception('请选择卫生评分 ');
        if (!$cost_performance_score)throw new \Exception('请选择性价比评分');
        $info = HouseEvaluate::get(['user_id'=>$user_id, 'order_id'=>$order_id]);
        if ($info)throw new \Exception('已评价');
        Db::startTrans();

        try {

            $evaluate_model = new HouseEvaluate;

            $data = [
                'house_id'=>$house_id,
                'user_id'=>$user_id,
                'order_id'=>$order_id,
                'owner_id'=>$owner_id,
                'content'=>$content,
                'images'=>$images,
                'video'=>$video,
                'describe_score'=>$describe_score,
                'location_score'=>$location_score,
                'convenient_score'=>$convenient_score,
                'service_score'=>$service_score,
                'health_score'=>$health_score,
                'cost_performance_score'=>$cost_performance_score
            ];

            $res = $evaluate_model->save($data);
            if ($res){

                HouseOrder::update(['is_user_evaluate'=>1],['order_id'=>$order_id]);
                House::where(['house_id'=>$house_id])->setInc('comment_num');
                $fen_data = [
                    'describe_score'=>$data['describe_score'],
                    'location_score'=>$data['location_score'],
                    'convenient_score'=>$data['convenient_score'],
                    'service_score'=>$data['service_score'],
                    'health_score'=>$data['health_score'],
                    'cost_performance_score'=>$data['cost_performance_score']
                ];
                $this->houseEvaluate($order_id,$fen_data);
                Db::commit();
                return true;
            } else {
                Db::rollback();
                throw new \Exception('评价失败');
            }


        } catch (\Exception $e) {
            // 回滚事务
            Db::rollback();
            throw new \Exception($e->getMessage().'-'.$e->getLine());
        }

    }

    public function houseEvaluate($order_id, $evaluate)
    {

        $order_info = HouseOrder::where(['order_id'=>$order_id])->field('owner_id,house_id')->find();

        $house_id = $order_info["house_id"];
        //$num = model("order_goods")->getCount([["order_id", "=", $order_id]], "order_goods_id");
        $num = HouseEvaluate::where(['order_id'=>$order_id])->count();
        $count = HouseEvaluate::where(['order_id'=>$order_id])->count();

        $house_info = House::where(['house_id'=>$house_id])
            ->field('house_id,describe_score,location_score,convenient_score,service_score,health_score,cost_performance_score')
            ->find();
        $last_count = $count + $num;
        //控制非法分值

        foreach ($evaluate as $k => $v) {
            if ($v < 60) {
                $v = 60;
            }
            if ($v > 100) {
                $v = 100;
            }
            $evaluate[$k] = $v;
        }
        $data = [];
        if ($evaluate["describe_score"] > 0) {
            $avg_desccredit = ($house_info["describe_score"] * $count + $evaluate["describe_score"] * $num) / $last_count;
            $data["describe_score"] = $avg_desccredit;
        }
        if ($evaluate["location_score"] > 0) {
            $avg_servicecredit = ($house_info["location_score"] * $count + $evaluate["location_score"] * $num) / $last_count;
            $data["location_score"] = $avg_servicecredit;
        }
        if ($evaluate["convenient_score"] > 0) {
            $avg_servicecredit = ($house_info["convenient_score"] * $count + $evaluate["convenient_score"] * $num) / $last_count;
            $data["convenient_score"] = $avg_servicecredit;
        }
        if ($evaluate["service_score"] > 0) {
            $avg_servicecredit = ($house_info["service_score"] * $count + $evaluate["service_score"] * $num) / $last_count;
            $data["service_score"] = $avg_servicecredit;
        }
        if ($evaluate["health_score"] > 0) {
            $avg_servicecredit = ($house_info["health_score"] * $count + $evaluate["health_score"] * $num) / $last_count;
            $data["health_score"] = $avg_servicecredit;
        }
        if ($evaluate["cost_performance_score"] > 0) {
            $avg_servicecredit = ($house_info["cost_performance_score"] * $count + $evaluate["cost_performance_score"] * $num) / $last_count;
            $data["cost_performance_score"] = $avg_servicecredit;
        }

        $total_score = $evaluate["cost_performance_score"]+$evaluate["health_score"]+$evaluate["service_score"]+$evaluate["convenient_score"]+$evaluate["location_score"]+$evaluate["describe_score"];
        $data['score'] = bcdiv($total_score,6,0);
        if (!empty($data)) {
            $result = House::update($data,['house_id'=>$house_id]);

        } else {
            $result = 1;
        }

        return $result;
    }
}
